param(
    [string] $protoPath = "..\..\proto",
    [string] $out = "Remoting",
    [string] $services = "no_client",
    [Parameter(Mandatory=$true)]
    [string[]] $protos = $(throw "Please specify protos")
)

$grpcToolsPath = "$env:USERPROFILE\.nuget\packages\grpc.tools"
if (!( Test-Path $grpcToolsPath )) {
    throw "Please install Grpc.Tools"
}
$grpcToolsVersion = Get-ChildItem $grpcToolsPath | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object -Descending | Select-Object -First 1

$grpcToolsLatest = Join-Path $grpcToolsPath $grpcToolsVersion.Name
$grpcToolsLatest = Join-Path $grpcToolsLatest "tools\windows_x64"
$protoc = Join-Path $grpcToolsLatest "protoc.exe"
$csharpPlugin = Join-Path $grpcToolsLatest "grpc_csharp_plugin.exe"
Write-Host "Latest Grpc.Tools: $grpcToolsLatest"

ForEach ($p in $protos){
    Write-Host "Generating $p"
    & $protoc --proto_path $protoPath --csharp_out $out  $p  --grpc_out=${services}:$out --plugin=protoc-gen-grpc=$csharpPlugin
}

Write-Host "Completed"